<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    //登录页显示
    public function index(Request $request){
        if($request->isMethod('post')){
//        dd($request->all());
            //用户名密码校验
            $rules = [
              'std_name'=>'required',
              'password'=>'required',
            ];
            $notices = [
              'std_name.required'=>  '用户名不能为空',
              'password.required' => '密码不能为空'
            ];
            $form_data = $request->all();
            //制作校验实例
            $validator = \Validator::make($form_data,$rules,$notices);
            //判断验证是否通过
            if($validator->passes()){
                //验证通过  检验用户名和密码是否正确
                //接收用户名和密码 用于attempt检查
                //only（在附录--集合） 返回集合中指定键的集合
                $userinfo = $request->only('std_name','password');

                if(\Auth::guard('front')->attempt($userinfo)){
                    return redirect()->intended('/');
                }else{
                    return redirect('home/login/index')
                        ->withErrors(['errorinfo'=>'用户名或密码错误'])
                        ->withInput();
                }
            }else{
                return redirect('home/login/index')
                    ->withErrors($validator)
                    ->withInput();
            }

        }
        return view('home.login.index');
    }
    //退出登录
    public function logout(){
        \Auth::guard('front')->logout();
        return redirect('/');
    }
}
